package com.atguigu.aclservice.service.impl;

import com.atguigu.aclservice.entity.User;
import com.atguigu.aclservice.service.PermissionService;
import com.atguigu.aclservice.service.UserService;
import com.hzx.security.entity.SecurityUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userDetailsService")
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        //根据用户名查询数据库
        User user = userService.selectByUsername(s);

        if(user == null)
            throw  new UsernameNotFoundException("用户不存在");

        com.hzx.security.entity.User curUser = new com.hzx.security.entity.User();
        BeanUtils.copyProperties(user,curUser);

        List<String> permissions = permissionService.selectPermissionValueByUserId(user.getId());

        SecurityUser securityUser = new SecurityUser(curUser);
        securityUser.setPermissionValueList(permissions);
        return securityUser;


    }
}
